﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Meqs.Logic.Core
{
    /// <summary>
    /// 数据库代理类
    /// </summary>
    internal class DBAccessProxy : DBAccess
    {
        /// <summary>
        /// 实际操作接口
        /// </summary>
        private DBAccess _IDBAccess = null;
        //
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="access"></param>
        public DBAccessProxy(DBAccess access)
            : base(access.DBType)
        {
            _IDBAccess = access;
        }
        //
        /// <summary>
        /// 初始化连接
        /// </summary>
        /// <param name="strServer"></param>
        /// <param name="port"></param>
        /// <param name="strDataBase"></param>
        /// <param name="strUser"></param>
        /// <param name="strPwd"></param>
        public override void InitConn(string strServer, string port, string strDataBase, string strUser, string strPwd)
        {
            _IDBAccess.InitConn(strServer, port, strDataBase, strUser, strPwd);
        }
        //
        /// <summary>
        /// 初始化连接
        /// </summary>
        /// <param name="strServer"></param>
        public override void InitConn(string strServer)
        {
            _IDBAccess.InitConn(strServer);
        }
        //
        /// <summary>
        /// 获得CMD
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public override DbCommand GetCmd(string strCmd)
        {
            return _IDBAccess.GetCmd(strCmd);
        }
        //
        /// <summary>
        /// 获得数据适配器
        /// </summary>
        public override DbDataAdapter DbAdp
        {
            get { return _IDBAccess.DbAdp; }
        }
        /// <summary>
        /// 更新表
        /// </summary>
        /// <param name="strTblName"></param>
        /// <param name="dt"></param>
        /// <param name="pk"></param>
        /// <returns></returns>
        public override int UpdateDataTable(string strTblName, DataTable dt, string pk)
        {
            return _IDBAccess.UpdateDataTable(strTblName, dt, pk);
        }
        //
        /// <summary>
        /// 获取所有表的信息
        /// </summary>
        public override DataTable Tables
        {
            get
            {
                DataTable dt = null;
                //try
                //{
                dt = _IDBAccess.Tables;
                //}
                //catch (Exception ex)
                //{
                //    ThrowException(ex);
                //}
                return dt;
            }
        }
        //
        /// <summary>
        /// 获得所有列的信息
        /// </summary>
        /// <returns></returns>
        public override DataTable GetColumns()
        {
            DataTable dt = null;
            //try
            //{
            dt = _IDBAccess.GetColumns();
            //}
            //catch (Exception ex)
            //{
            //    ThrowException(ex);
            //}
            return dt;
        }
        //
        /// <summary>
        /// 获得某张表的列信息
        /// </summary>
        /// <param name="strTable"></param>
        /// <returns></returns>
        public override DataTable GetColumns(string strTable)
        {
            DataTable dt = null;
            //try
            //{
            dt = _IDBAccess.GetColumns(strTable);
            //}
            //catch (Exception ex)
            //{
            //    ThrowException(ex);
            //}
            return dt;
        }
        //
        /// <summary>
        /// 打开
        /// </summary>
        public override void Open()
        {
            _IDBAccess.Open();
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public override void Close()
        {
            _IDBAccess.Close();
        }
        /// <summary>
        /// 执行非查询sql
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public override int RunNoQuery(string strCmd)
        {
            return _IDBAccess.RunNoQuery(strCmd); 
        }
        /// <summary>
        /// 获取字段最大值
        /// </summary>
        /// <param name="strTable"></param>
        /// <param name="strField"></param>
        /// <returns></returns>
        public override int GetFiledMax(string strTable, string strField)
        {
            return _IDBAccess.GetFiledMax(strTable, strField);
        }
        /// <summary>
        /// 执行非查询存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="ParaValue"></param>
        /// <param name="ParaName"></param>
        /// <param name="ParaType"></param>
        /// <param name="ParaDire"></param>
        public override void ExecProcNonQuery(string procName, object[] ParaValue, string[] ParaName, DBParamerType[] ParaType, DBParameterDirection[] ParaDire)
        {
            //try
            //{
            _IDBAccess.ExecProcNonQuery(procName, ParaValue, ParaName, ParaType, ParaDire);
            //}
            //catch (Exception ex)
            //{
            //    ThrowException(ex);
            //}
        }
        /// <summary>
        /// 执行查询存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="ParaValue"></param>
        /// <param name="ParaName"></param>
        /// <param name="ParaType"></param>
        /// <param name="ParaDire"></param>
        /// <returns></returns>
        public override DataTable ExecProcQuery(string procName, object[] ParaValue, string[] ParaName, DBParamerType[] ParaType, DBParameterDirection[] ParaDire)
        {
            DataTable dt = null;
            //try
            //{
            dt = _IDBAccess.ExecProcQuery(procName, ParaValue, ParaName, ParaType, ParaDire);
            //}
            //catch (Exception ex)
            //{
            //    ThrowException(ex);
            //}
            return dt;
        }
        /// <summary>
        /// 执行非查村
        /// </summary>
        /// <param name="strCmd"></param>
        /// <param name="name"></param>
        /// <param name="image"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public override int RunNoQuery(string strCmd, string name, byte[] image, long len)
        {
            return _IDBAccess.RunNoQuery(strCmd, name, image, len);
        }
        /// <summary>
        /// 执行非查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <param name="name"></param>
        /// <param name="paramer"></param>
        /// <returns></returns>
        public override int RunNoQuery(string strCmd, string[] name, object[] paramer)
        {
            return _IDBAccess.RunNoQuery(strCmd, name, paramer);
        }
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public override DataTable RunQuery(string strCmd)
        {
            return _IDBAccess.RunQuery(strCmd);
        }
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="strCmd"></param>
        /// <param name="name"></param>
        /// <param name="paramer"></param>
        /// <returns></returns>
        public override DataTable RunQuery(string strCmd, string[] name, object[] paramer)
        {
            return _IDBAccess.RunQuery(strCmd, name, paramer);
        }
        /// <summary>
        /// 执行非查询
        /// </summary>
        /// <param name="insertStrCmd"></param>
        /// <param name="name"></param>
        /// <param name="paramer"></param>
        /// <param name="newNum"></param>
        /// <returns></returns>
        public override int RunNoQuery(string insertStrCmd, string[] name, object[] paramer, ref int newNum)
        {
            return _IDBAccess.RunNoQuery(insertStrCmd, name, paramer, ref newNum);
        }
        /// <summary>
        /// 开始执行事务
        /// </summary>
        /// <returns></returns>

        public override object StartTransaction()
        {

            //try
            //{
            _Sign = _IDBAccess.StartTransaction();
            //}
            //catch (Exception ex)
            //{
            //    ThrowException(ex);
            //}
            return _Sign;
        }
        /// <summary>
        /// 提交事务
        /// </summary>
        /// <param name="sign"></param>
        public override void CommitTransaction(object sign)
        {
            _IDBAccess.CommitTransaction(sign);
        }
        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <param name="sign"></param>
        public override void RollBackTransaction(object sign)
        {
            _IDBAccess.RollBackTransaction(sign);

        }

    }
}